﻿using Conmmon;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace RabbitMQLearning.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ConmmodityContorller : ControllerBase
    {
        /// <summary>
        /// 购买商品
        /// </summary>
        /// <returns></returns>
        [HttpGet("PurchaseConmmodity")]
        public void PurchaseConmmodity(string message)
        {
            Dictionary<string, object> props = new Dictionary<string, object>();
            props.Add("x-dead-letter-exchange", "Purchase_Conmmodity_Dead");
            props.Add("x-dead-letter-routing-key", "Purchase_Dead");

            for (int i = 0; i < 100; i++)
            {
                RabbitMQHelper.Producer($"商品{i}", "Purchase_Conmmodity", "", "", props, 1000 * 20);
                Console.WriteLine($"商品{i}添加成功");
            }
        }

        /// <summary>
        /// 读取队列生成订单
        /// </summary>
        /// <returns></returns>
        [HttpGet("CreateOrder")]
        public void CreateOrder()
        {

            Dictionary<string, object> props = new Dictionary<string, object>();
            props.Add("x-dead-letter-exchange", "Purchase_Conmmodity_Dead");
            props.Add("x-dead-letter-routing-key", "Purchase_Dead");
            var returns = RabbitMQHelper.Consunmer("Purchase_Conmmodity", "", "", props);

            returns.OnCallback = (res) =>
            {
                try
                {
                    if (HelloProducer._conmmodityInfos.Count > 0)
                    {
                        HelloProducer._conmmodityInfos.RemoveAt(0);
                        Console.WriteLine($"{res.Content}_签收成功");
                        return true;
                    }
                    else
                    {
                        Console.WriteLine("签收失败，商品数量不足");
                        return false;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine($"签收失败，商品数量不足:{e}");
                    return false;
                }
            };
        }

        /// <summary>
        /// 错误订单队列
        /// </summary>
        /// <returns></returns>
        [HttpGet("NotCreateOrder")]
        public string NotCreateOrder()
        {
            var returns = RabbitMQHelper.Consunmer("Purchase_Conmmodity-delay", "Purchase_Conmmodity_Dead", "Purchase_Dead", null);
            returns.OnCallback = (res) =>
            {
                Console.WriteLine($"错误订单_{res.Content}_签收成功");
                return true;
            };
            return "";
        }

        /// <summary>
        /// 测试docker发布项目
        /// </summary>
        /// <returns></returns>
        [HttpGet("TestDockerIssue")]
        public string TestDockerIssue()
        {
            
            
            return DateTime.Now.ToString("yyyyMMddHHmmssffff") +" == "+ DateTime.Now.ToString("yyyyMMddHHmmssffff").Length;
        }


        [HttpPost("GetStudent")]
        public Student GetStudent(Student student)
        {
            return student;
        }

    }

    public class Student
    {
        public string Name { get; set; }

        public int Age { get; set; }
    }

    public class RequstMsg<T>
    {
        public RequstMsg()
        {
            Code = 200;
        }

        /// <summary>
        /// 数据
        /// </summary>
        public T Data { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 编码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 返回结果
        /// </summary>
        public bool Success { get; set; }
    }
}